liwe.form.instance.prototype.calendar5 = function ( vars )
{
	vars = vars || {};
	vars [ 'popup' ] = true;

	var cal = WWL.calendar5 ( this.name + '-' + vars [ 'name' ], vars );
	cal._container = cal.name + '-container';

	this._start_field ( vars );

	this.html += cal.to_string ();

	this._newline ( vars );

	this._widgets [ vars [ 'name' ] ] = cal;
};


liwe.form5.templates [ 'calendar' ] = '<span id="%(calendar_id)s" class="calendar"></span>';
liwe.form5.plugin_add ( 'calendar', function ( frm, type, name, attrs )
{
   
	frm.addEventListener ( 'render', function ()
	{
		attrs [ 'calendar' ].render ( attrs [ 'calendar_id' ]);
	} );

	attrs [ 'template' ] = 'calendar';
	attrs [ 'render' ] = function ( form, item )
	{
		item = dict ( item );
		item [ 'calendar_id' ]  = 'calendar-' + item [ 'name' ];

		var vars = dict ();
		vars [ 'popup' ] = true
		vars [ 'time' ] = item.get ( 'time', false );
		vars [ 'value' ] = item [ 'value' ];
		item [ 'calendar' ] = new WWL.calendar5 ( item [ '_id' ] , vars );
		return String.formatDict ( liwe.form5.templates [ 'calendar' ], item );
	};
	
	attrs [ 'get_value' ] = function ()
	{
		if ( attrs [ 'calendar' ] )
			return attrs [ 'calendar' ].get_value ();
		return attrs [ 'value' ];
	};
	attrs [ 'set_value' ] = function ( v )
	{
		attrs [ 'value' ] = v;
		if ( attrs [ 'calendar' ] )
			attrs [ 'calendar' ].set_value ( v );
	};
} );
      
